package supermemo.dictcn;
/*
 *
 * Copyright (c) 2004-2010.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY ARIZONA STATE UNIVERSITY ``AS IS'' AND
 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ARIZONA STATE UNIVERSITY
 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.Vector;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 * @author qyan
 *
 */
public class WordFileConverter 
{
	private List<EngWord> word_list = new Vector<EngWord>();
	private Set<String> time_list = new TreeSet<String>();
	
	/**
	 * Constructor 1
	 */
	public WordFileConverter(final String word_xml_file) 
	{
		import_from_file(word_xml_file);
	}

	/**
	 * Read words from the XML file exported from Dict.cn. The file is in the format of:  
	 <dict>
	  	<memo>
			<word>mnemonics</word>
			<pron>ni:'m&#596;niks</pron>
			<description><![CDATA[n. 记忆术]]></description>
			<time>2010-05-09</time>
			<level>1</level>
		</memo>
		<memo>
			<word>immolation</word>
			<pron>im&#601;'lei&#643;&#601;n</pron>
			<description><![CDATA[n. 牺牲，供奉牲礼，祭物]]></description>
			<time>2010-05-06</time>
			<level>1</level>
		</memo>
	 </dict>
	 * 
	 * @param word_txt_file
	 */
	private void import_from_file(final String word_txt_file)
	{
		SAXReader reader = new SAXReader();
	    try
		{
			Document document = reader.read(word_txt_file);
			
			List<Element> elm_list = document.getRootElement().elements();
			
			for(Element cur_elm : elm_list)
			{
				word_list.add(new EngWord(cur_elm));
			}
			
			///
			List<Element> time_elm_list = document.selectNodes("//time");
			for(Element cur_elm : time_elm_list)
			{
				time_list.add(cur_elm.getText());
			}
//			System.out.println(time_list);
			
		} catch(DocumentException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public Set<String> getTimeSet()
	{
		return time_list;
	}
	
	/**
	 * Frame the right XML document for the SuperMem!
	 * @return
	 */
	private Document create_doc(String earliest_time)
	{
		Document document = DocumentHelper.createDocument();
		//1. root element
        Element root = document.addElement( "SuperMemoCollection" );
        //2. count of elements
        root.addElement("Count").addText(""+(word_list.size()+1));
        
        //3. topic information
        Element word_elm = root.addElement("SuperMemoElement");
        word_elm.addElement("ID").addText("1");
        word_elm.addElement("Title").addText("Dict.cn");
        word_elm.addElement("Type").addText("Topic");
        
        //4. enter the words
        int id = 2;
        for(EngWord cur_word : word_list)
        {
        	if(!earliest_time.isEmpty() && earliest_time.compareTo(cur_word.getTimeStamp())>0) continue;
        	Element cur_elm = word_elm.addElement("SuperMemoElement");
        	cur_elm.addElement("ID").addText(""+id++);
        	cur_elm.addElement("Type").addText("Item");
        	
        	Element cur_content = cur_elm.addElement("Content");
        	cur_content.addElement("Question").addText(cur_word.getWord());
        	cur_content.addElement("Answer").addText(cur_word.getDescription());
        }
        
        return document;
	}
	
	/**
	 * Export the created document to an XML file. 
	 * @param out_xml_file
	 */
	public void export_to_file(final String out_xml_file, final String earliest_time)
	{
		XMLWriter writer;
		try
		{
			writer = new XMLWriter(new FileWriter(out_xml_file));
			writer.write(create_doc(earliest_time));
	        writer.close();
	        
		} catch(IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		System.out.println("Welcome to the class WordCollector!");
		WordFileConverter wc = new WordFileConverter("data/wordbook2.xml");
		wc.export_to_file("data/word4superMem.xml", "");
	}

}
